﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BA2009.DataAccess.Context;
using BA2009.Entities;
using System.Data.Linq;
//using BA2009.DataAccess.Entities;

namespace BA2009.Data.DataAccess
{
    public sealed class PageRepository :RepositoryBase<Page>, IPageRespository
    {


        #region Backup
        //private BA2009Context dbContext = new BA2009Context();

        ///// <summary>
        ///// Find all Page
        ///// </summary>
        ///// <returns></returns>
        //public IQueryable<BA2009.Entities.Page> FindAll()
        //{
        //    var listPage = from p in dbContext.Pages
        //                   select new BA2009.Entities.Page
        //                   {
        //                       PageID = p.PageID,
        //                       PageName = p.PageName,
        //                       Description = p.Description,
        //                       Code = p.Code
        //                   };
        //    return listPage;


        //}
        ///// <summary>
        ///// Return the page from list of pages by pagId
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public BA2009.Entities.Page GetById(int id)
        //{
        //    var data = from p in dbContext.Pages
        //               where p.PageID == id
        //               select new BA2009.Entities.Page
        //               {
        //                   PageID = id,
        //                   PageName = p.PageName,
        //                   Description = p.Description
        //               };

        //    return data.SingleOrDefault();
        //}

        ///// <summary>
        ///// Add new page to list of pages
        ///// </summary>
        ///// <param name="item"></param>
        //public void Add(BA2009.Entities.Page item)
        //{
        //    Page page = new Page();
        //    page.PageID = item.PageID;
        //    page.PageName = item.PageName;
        //    page.Description = item.Description;

        //    dbContext.Pages.InsertOnSubmit(page);
        //}

        ///// <summary>
        ///// Delete the page from list of pages
        ///// </summary>
        ///// <param name="item"></param>
        //public void Delete(BA2009.Entities.Page item)
        //{
        //    Page page = new Page();
        //    page.PageID = item.PageID;
        //    page.PageName = item.PageName;
        //    page.Description = item.Description;

        //    dbContext.Pages.DeleteOnSubmit(page);
        //}

        ///// <summary>
        ///// Persistence
        ///// </summary>
        //public void Save()
        //{
        //    dbContext.SubmitChanges();
        //}
        #endregion


        protected override void Save(object linqEnity, BA2009Context dbContext, params object[] maps)
        {
            
                var page = linqEnity as BA2009.DataAccess.Entities.Page;
                dbContext.Pages.InsertOnSubmit(page);
                dbContext.SubmitChanges();

            
        }

        protected override void Delete(int id, BA2009Context dbContext)
        {

            var page = (from b in dbContext.Pages
                              where b.PageID == id
                              select b).SingleOrDefault();
                dbContext.Pages.DeleteOnSubmit(page);
                dbContext.SubmitChanges();
            
        }

        protected override void Update(object itemToUpdate, object orginialItem, BA2009Context dbContext, params object[] maps)
        {
                var entity = itemToUpdate as BA2009.DataAccess.Entities.Page;
                var original = orginialItem as BA2009.DataAccess.Entities.Page;
                dbContext.Pages.Attach(entity, original);
                dbContext.SubmitChanges();
            
        }

        /// <summary>
        /// Get latest linq entity from database
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        protected override object GetLinqEntity(int id)
        {
            var data= (from b in db.Pages
                    where b.PageID == id
                    select b).First();

            db.Refresh(RefreshMode.OverwriteCurrentValues, data);

            return data;
        }



        public override IQueryable<Page> FindAll()
        {
            var listPage = from p in db.Pages
                           select new BA2009.Entities.Page
                           {
                               PageID = p.PageID,
                               PageName = p.PageName,
                               Description = p.Description,
                               Code = p.Code
                           };
            return listPage;
        }

        public override Page GetById(int id)
        {
            var data = from p in db.Pages
                       where p.PageID == id
                       select new BA2009.Entities.Page
                       {
                           PageID = p.PageID,
                           Code=p.Code,
                           PageName = p.PageName,
                           Description = p.Description
                       };

            return data.SingleOrDefault();
        }
    }
}
